<?php


namespace app\api\controller;


use app\common\controller\CommonBase;
use app\common\model\Parkinfo;
use app\common\model\Paylog;
use etpcar\EtpCar;
use think\Controller;
use think\Request;
use Wechat\Wxobjopen;

class Carapi extends Controller
{
    public function notify(Request $request)
    {
        $postStr = $request->param();
        if ($postStr) {
            file_put_contents('/data2/html/apis/1.txt', "[" . date('Y-m-d H:i:s') . "]" . json_encode($postStr) . PHP_EOL, 8);
            $param = $postStr;
            $CommonBase=new CommonBase();
            $parkinfo=$CommonBase->getparkinfo($param['parkno']);
            $option = [
                'key' => $parkinfo['parkkey'],
                'parkno' => $parkinfo['parkno'],
            ];
            $Etpcar = new EtpCar($option);
            $res = $Etpcar->token_query($param);
            if ($res == "success") {
                $Carnotice = new Carnotice();
                switch ($param['taskname']) {
                    case "waitout":
                        $res = $Carnotice->wait_out($param,$parkinfo);
                        if ($res['ReMsg']['ErrNo'] == "0000") {
                            $CarPay = new CarPay();
                            $res = $CarPay->get_car_fee($param,$parkinfo);
                        }
                        break;
                    case "incar":
                        $res = $Carnotice->incar($param,$parkinfo);
                        break;
                    case "outcar":
                        $res = $Carnotice->outcar($param,$parkinfo);
                        break;

                }
                file_put_contents('/data2/html/apis/2.txt', "[" . date('Y-m-d H:i:s') . "]" . json_encode($res) . PHP_EOL, 8);
                return json($res);
            } else {
                return "FAIL";
            }
        } else {
            return "FAIL";

        }
    }


    public function test(Request $request)
    {
//        var_dump($request);
//        exit();
//        $postStr='{"parkno":"18568816812","taskname":"waitout","opertype":"add","tasktime":"20190422150327","taskapi":"\/app\/getwaitout.aspx","taskkey1":"carno","key1value":"\u8c6be1m926","taskkey2":"outtime","key2value":"20190422150327","Token":"f3ffc5b9e945252ce98c474f4b8faf05"}';
//        $postStr='{"parkno":"18568816812","taskname":"waitout","opertype":"add","tasktime":"20190422114739","taskapi":"\/app\/getwaitout.aspx","taskkey1":"carno","key1value":"\u8c6be1m926","taskkey2":"outtime","key2value":"20190422114739","Token":"308b8b0f25d8afe8f6ab89f8ce8c8f8f"}';
        $postStr='{"parkno":"18568816812","taskname":"outcar","opertype":"add","tasktime":"20190424195947","taskapi":"\/app\/getoutcar.aspx","taskkey1":"carno","key1value":"\u8c6be1m926","taskkey2":"outtime","big_tatolset":"999","big_nullset":"988","mini_tatolset":"0","mini_nullset":"0","key2value":"20190424195947","Token":"7703fc6c671b5700115a5ee005b63537"}';
        $param = json_decode($postStr, 256);
        $option = [
            'key' => "13E016AC",
            'parkno' => "18568816812",
        ];
        $CommonBase=new CommonBase();
        $parkinfo=$CommonBase->getparkinfo($param['parkno']);
//        $param['parkno']='18568816812';
//        $param['key1value']='豫E1M926';
        $Etpcar = new EtpCar($option);
        $res = $Etpcar->token_query($param);
        if ($res == "success") {
            $Carnotice = new Carnotice();
            switch ($param['taskname']) {
                case "waitout":
                    $res = $Carnotice->wait_out($param,$parkinfo);
                    if ($res['ReMsg']['ErrNo'] == "0000") {
                        $CarPay = new CarPay();
                        $res = $CarPay->get_car_fee($param,$parkinfo);
                        var_dump($res);
                        exit();
                    }
                    break;
                case "incar":
                    $res = $Carnotice->incar($param,$parkinfo);
                    var_dump($res);
                    exit();
                    break;
                case "outcar":
                    $res = $Carnotice->outcar($param,$parkinfo);
                    var_dump($res);
                    exit();
                    break;

            }
        }
        var_dump($res);
    }

    public function wxvehiclenotify()
    {
        $postStr = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        if (isset($msg['return_code'])) {

            $pay_zt = $msg['return_code'];
            if ($pay_zt == "SUCCESS") {
                $CarPay = new CarPay();
                $CarPay->wxvehicle_order_query($msg['out_trade_no']);
            }
        }
        file_put_contents('/data2/html/apis/4.txt', "[" . date('Y-m-d H:i:s') . "]" . json_encode($msg) . PHP_EOL, 8);

        $su = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        echo $su;
    }

}